package com.lq.schoolshop.controller.before;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lq.schoolshop.bean.Result;
import com.lq.schoolshop.bean.StatusCode;
import com.lq.schoolshop.controller.BaseController;
import com.lq.schoolshop.pojo.*;
import com.lq.schoolshop.pojo.Expand.CategoryTypeAndGoods;
import com.lq.schoolshop.pojo.Expand.TaskAwayParamExample;
import com.lq.schoolshop.service.CategoryTypeService;
import com.lq.schoolshop.service.ShopCarService;
import com.lq.schoolshop.service.StoreService;
import com.lq.schoolshop.util.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * @Author
 * @Date2021/1/23 15:21
 * @Version V1.0
 * 外卖详情页
 **/
@Api(value = "商城前台功能--外卖店铺模块",tags = "商城前台功能--外卖店铺模块")
@RequestMapping("takeaway")
@Controller
public class ForTakeAwayController extends BaseController {

    @Autowired
    private CategoryTypeService categoryTypeService;

    @Autowired
    private StoreService storeService;

    @Autowired
    private ShopCarService shopCarService;

    //1.根据分类ID显示商品信息
    @ApiOperation(value = "根据分类ID显示商品信息",notes = "显示外卖店铺根据分类ID显示商品信息")
    @ApiImplicitParam(name = "CategoryID",value = "分类ID",dataType = "String",required = true,paramType = "query")
    @ResponseBody
    @RequestMapping(value = "/ShowGoodsByCategoryID",method = RequestMethod.GET)
    public Result ShowAll(@RequestParam("CategoryID") int CategoryID
        ,@RequestParam("pageNum") int pageNum,@RequestParam("pageSize") int pageSize){
        Result result = null;
        PageHelper.startPage(pageNum,pageSize);
        List<Goods> goods = categoryTypeService.selgoodsByCategoryId(CategoryID);
        PageInfo pageInfo = new PageInfo(goods);
        result = new Result(true, StatusCode.OK,"成功",pageInfo);
        return result;
    }

    //2.显示外卖店铺的分类信息 和外卖店铺的店铺信息
    @ApiOperation(value = "显示外卖店铺分类信息",notes = "显示外卖店铺全部分类信息")
    @ApiImplicitParam(name = "storeid",value = "店铺ID",dataType = "String",required = true,paramType = "query")
    @ResponseBody
    @RequestMapping(value = "/ShowAllCategory",method = RequestMethod.GET)
    public Result ShowAllCategory(@RequestParam("storeid") int storeid){
        Result result = null;
        CategoryTypeAndStores categoryTypeAndStores = new CategoryTypeAndStores();
        List<CategoryType> categoryTypes = categoryTypeService.selcategorytypeAll();
        List<Store> stores = storeService.selById(storeid);
        categoryTypeAndStores.setCategoryTypes(categoryTypes);
        categoryTypeAndStores.setStores(stores);
        result = new Result(true, StatusCode.OK,"成功",categoryTypeAndStores);
        return result;
    }

    //3.将外卖店铺的商品加入购物车
    @ApiOperation(value = "商品加入购物车",notes = "将外卖店铺的商品加入购物车")
    @ApiImplicitParam(name = "tmap",value = "商品ID和数量",dataType = "HashMap",required = true,paramType = "query")
    @ResponseBody
    @RequestMapping(value = "/InsertCategoryToCar",method = RequestMethod.POST)
    public Result InsertCategoryToCar(@RequestHeader("define-token") String language,@RequestBody Map<Integer,Integer>tmap){
        Result result = null;
        int index = 0;
        int userint = getUserIDByToken(language);
        boolean empty = tmap.isEmpty();
        if(!empty) {  //如果不为空
            List<Integer> mapKeyList = new ArrayList<>(tmap.keySet());
            Object[] objects = mapKeyList.toArray();
            Integer[] integers = Arrays.copyOfRange(objects, 0, objects.length, Integer[].class);
            Integer[] resultByGid = shopCarService.selAllById(integers);
            //购物车找到了该商品信息
            for(Integer key:tmap.keySet()) {
                if(index < resultByGid.length){
                    if(key == resultByGid[index++]){
                        shopCarService.updateGoodsCount(tmap.get(key), key); //存在该商品信息
                    }else {
                        shopCarService.insertAll(userint, key, tmap.get(key)); //不存在该商品信息
                    }
                }else {
                    shopCarService.insertAll(userint, key, tmap.get(key));   //不存在该商品信息
                } }
            result = new Result(true,StatusCode.OK,"成功");
        }else {  //如果为空
            result = new Result(true,StatusCode.OK,"失败");
        }
        return result;
    }

}
